home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 9- full screen mode / completed source / animalpane.java next >
Encoding:
Java Source  |  2000-09-28  |  15.0 KB  |  488 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4. import java.util.Hashtable;
  5.  
  6. import quicktime.app.QTFactory;
  7. import quicktime.app.anim.Compositor;
  8. import quicktime.app.anim.TwoDSprite;
  9. import quicktime.app.event.QTActionEvent;  
  10. import quicktime.app.event.QTActionListener;
  11. import quicktime.app.event.QTMouseTargetController; 
  12. import quicktime.app.image.GraphicsImporterDrawer; 
  13. import quicktime.app.image.ImagePresenter; 
  14. import quicktime.app.image.ImageDataSequence;
  15. import quicktime.app.image.ImageUtil;
  16. import quicktime.app.ui.ReleaseButton;
  17. import quicktime.app.ui.ButtonActivator;
  18. import quicktime.app.players.MoviePresenter;
  19. import quicktime.app.players.QTPlayer;
  20.  
  21. import quicktime.io.QTFile;
  22. import quicktime.io.OpenMovieFile;
  23.  
  24. import quicktime.qd.QDRect;
  25. import quicktime.qd.QDGraphics;
  26. import quicktime.qd.QDColor; 
  27. import quicktime.qd.QDConstants; 
  28.  
  29. import quicktime.std.StdQTConstants;
  30. import quicktime.std.movies.Movie; 
  31. import quicktime.std.image.Matrix;
  32. import quicktime.std.image.GraphicsMode;
  33.  
  34. import quicktime.QTSession;
  35. import quicktime.QTException; 
  36.  
  37. /**
  38.  * QTZoo Module 9 - Using Full screen mode
  39.  *
  40.  * @author Levi Brown
  41.  * @author Michael Hopkins
  42.  * @author Apple Computer, Inc.
  43.  * @version 8.0 4/10/2000
  44.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  45.  *    
  46.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  47.  *                ("Apple") in consideration of your agreement to the following terms, and your
  48.  *                use, installation, modification or redistribution of this Apple software
  49.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  50.  *                please do not use, install, modify or redistribute this Apple software.
  51.  *
  52.  *                In consideration of your agreement to abide by the following terms, and subject
  53.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  54.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  55.  *                reproduce, modify and redistribute the Apple Software, with or without
  56.  *                modifications, in source and/or binary forms; provided that if you redistribute
  57.  *                the Apple Software in its entirety and without modifications, you must retain
  58.  *                this notice and the following text and disclaimers in all such redistributions of
  59.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  60.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  61.  *                Apple Software without specific prior written permission from Apple.  Except as
  62.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  63.  *                are granted by Apple herein, including but not limited to any patent rights that
  64.  *                may be infringed by your derivative works or by other works in which the Apple
  65.  *                Software may be incorporated.
  66.  *
  67.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  68.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  69.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  70.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  71.  *                COMBINATION WITH YOUR PRODUCTS.
  72.  *
  73.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  74.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  75.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  76.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  77.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  78.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  79.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  80.  * 
  81.  
  82.  */
  83. public class AnimalPane extends ZooPane
  84. {
  85.     /**
  86.      *  Public default constructor
  87.      *  Creates the map button, and sets up the listeners
  88.      */
  89.     public AnimalPane()
  90.     {
  91.         QDRect size = new QDRect(MainFrame.WIDTH, MainFrame.HEIGHT);
  92.         try
  93.         {
  94.             loadSound("data/zebra/Zebra.au");
  95.  
  96.             QDGraphics gw = new QDGraphics (size);
  97.             compositor = new Compositor (gw, QDColor.white, 30, 1);
  98.             
  99.             displayImage( "data/zebra/ZebraBackground.jpg", 6, 5, 110 );        
  100.                         
  101.             addText( "data/zebra/Zebra.txt", 5, 15, 65, 415, 220 );
  102.             
  103.             addMovie( "data/zebra/Zebra.mov", 2, 425, 65 );
  104.             
  105.             ctr = new QTMouseTargetController( false );
  106.             ctr.addQTMouseListener( new PaneMouseListener( QDColor.lightGray, QDColor.white, QDColor.darkGray));
  107.             compositor.addController( ctr );            
  108.                 
  109.              TwoDSprite currentSprite;
  110.              areas = new Hashtable(1);
  111.             currentSprite = CreateRollover( "data/MapButton.jpg", 5, 12, 406 );
  112.             areas.put(currentSprite, "map");    
  113.             
  114.             compositor.getTimer().setRate(0);    
  115.         }
  116.         catch ( QTException qte )
  117.         {
  118.             qte.printStackTrace();
  119.         }
  120.     }    
  121.     
  122.     /**
  123.      * Listener object for the Movie controller buttons
  124.      * Called when the button is released
  125.      */
  126.     public class ButtonListener implements QTActionListener    
  127.     {
  128.         /**
  129.          * Called when the button is released
  130.          * @param ???
  131.          */
  132.         public void actionPerformed( QTActionEvent event ) 
  133.         {
  134.             System.out.println( "AnimalPane.actionPerformed() " );
  135.             try
  136.             {
  137.                 Object source = event.getSource();
  138.                 
  139.                 if (source.equals(playButton))            // Play >
  140.                 {
  141.                     if ( md.getRate() == 0 )
  142.                     {
  143.                         md.setRate( 1 );
  144.                     }
  145.                     stopButton.setCurrentImage( stopRel );
  146.                     stopButton.setReleasedImage( stopRel );
  147.                     playButton.setCurrentImage( playPlaying );
  148.                     playButton.setReleasedImage( playPlaying );
  149.                 }                
  150.                 else if (source.equals(stopButton))        // Stop []
  151.                 {
  152.                     if ( md.getRate() == 1 )
  153.                     {
  154.                         md.setRate( 0 );
  155.                     }
  156.                     stopButton.setCurrentImage( stopDeactive );
  157.                     stopButton.setReleasedImage( stopDeactive );
  158.                     playButton.setCurrentImage( playRel );
  159.                     playButton.setReleasedImage( playRel );
  160.                 }
  161.                 else if (source.equals(rewindButton))    // Rewind <<
  162.                 {
  163.                     md.setTime(0);
  164.                 }
  165.             }
  166.             catch (QTException exc )
  167.             {
  168.                 exc.printStackTrace();
  169.             }
  170.         }
  171.     }
  172.     
  173.     /**
  174.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  175.      * @param ???
  176.      */
  177.     public ImagePresenter MakePresenterFromFile( String name )
  178.     {
  179.         try
  180.         {
  181.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  182.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  183.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  184.         }
  185.         catch (IOException e)
  186.         {
  187.             e.printStackTrace();
  188.         }
  189.         catch (QTException e)
  190.         {
  191.             e.printStackTrace();
  192.         }
  193.         return null;    
  194.     }
  195.     
  196.     /**
  197.      * Called to do any setup after being set as the client of the QTCanvas.
  198.      * May be used to start effects running, movies playing, etc.
  199.      */
  200.     public void start()
  201.     {
  202.         buttonController.addQTMouseListener(buttonActivator);
  203.         try
  204.         {
  205.             stopButton.setCurrentImage(  stopRel );        // Stop button should not have focus
  206.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  207.             playButton.setReleasedImage( playPlaying );
  208.         }
  209.         catch( QTException e )
  210.         {
  211.             e.printStackTrace();
  212.         }
  213.         try
  214.         {
  215.             md.setRate(1);                                // start the player
  216.         }
  217.         catch( QTException e )
  218.         {
  219.             e.printStackTrace();
  220.         }
  221.         compositor.getTimer().setRate(1);                // start the compositor
  222.     }
  223.     
  224.     /**
  225.      * Called to do clean up after being removed as the client of the QTCanvas.
  226.      * Should be used to stop effects running, movies playing, etc.
  227.      */
  228.     public void stop()
  229.     {
  230.         System.out.println( "Stop" );
  231.         buttonController.removeQTMouseListener(buttonActivator);
  232.         compositor.getTimer().setRate(0);
  233.         try
  234.         {
  235.             md.setRate(0);
  236.         }
  237.         catch( QTException e )
  238.         {
  239.             e.printStackTrace();
  240.         }
  241.     }
  242.     
  243.     /**
  244.      *  Plays the sound file associated with the current AnimalPane
  245.      */
  246.     public void playSound()
  247.     {
  248.         if (player == null)
  249.             return;
  250.  
  251.         try
  252.         {
  253.             player.setTime(0);            //Start the sound at the beginning
  254.             player.startTasking();         //Make sure the player gets time to play the sound
  255.             player.setRate(1);             //Start playing
  256.         }
  257.         catch (QTException exc)
  258.         {
  259.             exc.printStackTrace();
  260.         }
  261.     }
  262.     
  263.     /**
  264.      * Creates a Movie from the specified path and adds it
  265.      * to the compositor in the hardcoded location.
  266.      * @param moviePath the relative path to the movie file to add.
  267.      * @param layer, the layer of the compositor to add the movie to.
  268.      * @param x the x coordinate location.
  269.      * @param y the y coordinate location.
  270.      * @see #makeMovie
  271.      */
  272.     protected void addMovie(String moviePath, int layer, int x, int y)
  273.     {
  274.         try
  275.         {
  276.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  277.             md = new MoviePresenter (m);
  278.             md.setLocation (x, y);
  279.             // Add the Movie Controls
  280.             
  281.             playRel     = MakePresenterFromFile( "data/Play.jpg" );            // Play >
  282.             playPress   = MakePresenterFromFile( "data/PlayPressed.jpg" );
  283.             playPlaying = MakePresenterFromFile( "data/Playing.jpg" );
  284.             playButton = new MovieButton( playRel, playPress, playPlaying );
  285.             playButton.setLabel ("Play");
  286.             playButton.setLocation( x + 68, y + 152 );
  287.             
  288.             stopRel      = MakePresenterFromFile( "data/Stop.jpg" );        // Stop []
  289.             stopPress    = MakePresenterFromFile( "data/StopPressed.jpg" );
  290.             stopDeactive = MakePresenterFromFile( "data/Stopped.jpg" );
  291.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  292.             stopButton.setLabel ("Stop");
  293.             stopButton.setLocation( x + 136, y + 152 );
  294.             
  295.             rewRel      = MakePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  296.             rewPress    = MakePresenterFromFile( "data/RewindPressed.jpg" );
  297.             rewDeactive = MakePresenterFromFile( "data/Rewind.jpg" );
  298.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  299.             rewindButton.setLabel( "Rewind" );
  300.             rewindButton.setLocation( x, y + 152 );
  301.             
  302.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  303.             
  304.             playButton.addActionListener(bl);
  305.             stopButton.addActionListener(bl);
  306.             rewindButton.addActionListener(bl);
  307.             
  308.             compositor.addMember (md, layer);                                // add the movie presenter
  309.             compositor.addMember (playButton);                                //     and buttons to the compositor
  310.             compositor.addMember (stopButton);
  311.             compositor.addMember (rewindButton);
  312.             
  313.             buttonController = new QTMouseTargetController (false);    
  314.             buttonController.addMember(playButton);
  315.             buttonController.addMember(stopButton);
  316.             buttonController.addMember(rewindButton);
  317.         
  318.             compositor.addController (buttonController);
  319.             buttonActivator = new ButtonActivator();
  320.             
  321.             md.setRate(1);                                                    // start the movie        
  322.         }
  323.         catch (QTException exc)
  324.         {
  325.             exc.printStackTrace();
  326.         }
  327.         catch ( IOException exc )
  328.         {
  329.             exc.printStackTrace();
  330.         }    
  331.     }
  332.     
  333.     /**
  334.      * Utility routine loads an image from a file, renders it, and adds it to the compositor
  335.      * @param filename the name of the file to be displayed
  336.      * @param layer the layer in the compositor where the image will be displayed
  337.      * @param x the x coordinate of the top left corner where the image is to be displayed
  338.      * @param y the y coordinate of the top left corner where the image is to be displayed
  339.      */
  340.     protected void displayImage( String filename, int layer, int x, int y )
  341.     {
  342.         try
  343.         {    
  344.             QTFile file = new QTFile (QTFactory.findAbsolutePath (filename));
  345.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  346.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  347.             presenter.setLocation( x, y );
  348.             compositor.addMember(presenter, layer );
  349.         }
  350.         catch (QTException exc)
  351.         {
  352.             exc.printStackTrace();
  353.         }
  354.         catch (IOException exc)
  355.         {
  356.             exc.printStackTrace();
  357.         }        
  358.     }
  359.     
  360.     /**
  361.      * Reads a text file from the disk and displays it in the compositor
  362.      * @param textPath the relative path where the text is located
  363.      * @param layer, the layer of the compositor to add the text to.
  364.      * @param x the x coordinate location.
  365.      * @param y the y coordinate location.
  366.      * @param width the width of the area to display the text.
  367.      * @param height the height of the area to display the text.
  368.      */
  369.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  370.     {
  371.         try
  372.         {
  373.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  374.             
  375.             ImagePresenter presenter = text.getPresenter();
  376.             Matrix theMatrix = new Matrix();
  377.             theMatrix.translate( (float) x, (float) y );
  378.             presenter.setMatrix(theMatrix);
  379.             compositor.addMember(presenter, layer);
  380.         }
  381.         catch ( QTException exc )
  382.         {
  383.             exc.printStackTrace();
  384.         }
  385.         catch ( FileNotFoundException exc )
  386.         {
  387.             exc.printStackTrace();
  388.         }    
  389.     }
  390.     
  391.     /**
  392.      * Loads the specified sound file from disk and prepares it for playing
  393.      * @param ???
  394.      */
  395.     protected void loadSound(String soundPath)
  396.     {
  397.         try
  398.         {
  399.             String soundLocation = QTFactory.findAbsolutePath(soundPath).getPath();
  400.             //this call works with a file://, http://, rtsp://located movie
  401.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  402.         }
  403.         catch (IOException exc)
  404.         {
  405.             exc.printStackTrace();
  406.         }
  407.         catch (QTException exc)
  408.         {
  409.             exc.printStackTrace();
  410.         }
  411.     }
  412.     
  413.  
  414.     /**
  415.      * Opens the movie file and sets it up to be played.
  416.      * @param f a QTFile representing the movie to initialize.
  417.      * @return the movie, ready to play
  418.      * @see #addMovie
  419.      */
  420.     protected Movie makeMovie (QTFile f) throws IOException, QTException
  421.     {
  422.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  423.         Movie m = Movie.fromFile (movieFile);
  424.         m.getTimeBase().setFlags (StdQTConstants.loopTimeBase);    
  425.         return m;    
  426.     }
  427.     
  428.     /**
  429.      *    Creates a TwoDSprite that behaves as a simple rollover button (such as the map button)
  430.      */
  431.     protected TwoDSprite CreateRollover( String filename, int layer, int x, int y )
  432.     {
  433.         TwoDSprite sprite = null;
  434.         try
  435.         {
  436.             QTFile                 file = new QTFile( QTFactory.findAbsolutePath( filename ));
  437.             ImagePresenter         presenter = ImagePresenter.fromFile( file );
  438.             ImageDataSequence     dataSeq = new ImageDataSequence (presenter.getDescription());
  439.             
  440.             dataSeq.addMember( presenter.getImage() );
  441.             
  442.             if ((QTSession.isCurrentOS(QTSession.kWin32) && QTSession.getQTMajorVersion() == 3) == false)    //doesn't work on QT3.0.2 on Win
  443.                 dataSeq = ImageUtil.makeTransparent ( dataSeq, QDColor.white );        // recompress the image using white as the transparent color
  444.             
  445.             Matrix theMatrix = new Matrix();
  446.             theMatrix.translate( (float) x, (float) y );
  447.             sprite = new TwoDSprite( dataSeq, theMatrix, true, 10 );
  448.             
  449.             GraphicsMode normalGM = new GraphicsMode (QDConstants.blend, QDColor.lightGray);
  450.             sprite.setGraphicsMode (normalGM);
  451.             compositor.addMember(sprite, layer);
  452.             ctr.addMember( sprite );
  453.         }
  454.         catch ( QTException exc )
  455.         {
  456.             exc.printStackTrace();
  457.         }
  458.         catch ( IOException exc )
  459.         {
  460.             exc.printStackTrace();
  461.         }
  462.         
  463.         return sprite;    
  464.     }
  465.     
  466.     
  467.     protected QTPlayer player;
  468.     protected MoviePresenter md;
  469.     
  470.     protected ImagePresenter playRel;
  471.     protected ImagePresenter playPress;
  472.     protected ImagePresenter playPlaying;
  473.     protected MovieButton playButton;
  474.             
  475.     protected ImagePresenter stopRel;
  476.     protected ImagePresenter stopPress;
  477.     protected ImagePresenter stopDeactive;
  478.     protected MovieButton stopButton;
  479.     
  480.     protected ImagePresenter rewRel;
  481.     protected ImagePresenter rewPress;
  482.     protected ImagePresenter rewDeactive;
  483.     protected MovieButton rewindButton;
  484.     
  485.     protected QTMouseTargetController buttonController;
  486.     protected ButtonActivator buttonActivator;
  487. }
  488.